package com.hospital.registrationappointment.module.controller;

import com.alibaba.fastjson.JSON;
import com.hospital.common.core.exception.ExceptionHelpEnum;
import com.hospital.common.core.exception.ExceptionPrint;
import com.hospital.common.core.module.controller.vo.CancelReq;
import com.hospital.common.core.module.controller.vo.ReservationReq;
import com.hospital.common.core.result.SuccessTip;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.DateUtil;
import com.hospital.common.core.utils.RedisUtils;
import com.hospital.common.core.utils.ValidatedCheck;
import com.hospital.registrationappointment.module.service.ClinicSequenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author lvyao
 * @description 控制器
 * @date 2020/10/29
 */
@RestController
@RequestMapping("/register")
@Slf4j
@Validated
public class RegistrationAppointmentController {

    @Autowired
    private ClinicSequenService clinicSequenService;


    @PostMapping(value = "/reservation", produces = {"application/json;charset=UTF-8"})
    public Tip getAssessInitData(@RequestBody ReservationReq reservationReq, HttpServletRequest request) {
        Tip successTip = new SuccessTip();
        String startTime = DateUtil.getAllTime();
        StopWatch stopwatch2 = new StopWatch();
        stopwatch2.start();
        String register_key = "register_server";
        //锁住当前所有的需要修改的信息，只要此人未处理完，均锁住不让预约
        if (RedisUtils.tryGetDistributedLock(register_key, "1", 20000)) {
            try {
                successTip = ValidatedCheck.validateFunc(reservationReq);
                if (successTip.getCode() == 200) {
                    successTip = clinicSequenService.insertReservation(reservationReq);
                }

            } catch (Exception e) {
                successTip.setCode(ExceptionHelpEnum.REGIST_ERR.getCode());
                successTip.setData(e.getMessage());
            } finally {
                RedisUtils.releaseDistributedLock(register_key, "1");
                stopwatch2.stop();
                String endTime = DateUtil.getAllTime();
                ExceptionPrint.printCommErrorString("预约挂号",
                        JSON.toJSONString(reservationReq), String.valueOf(successTip.getCode())
                        , JSON.toJSONString(successTip.getMessage() + "=====" + successTip.getData()), startTime, endTime, String.valueOf(stopwatch2.getTotalTimeSeconds()));

            }
        } else {
            successTip.setCode(ExceptionHelpEnum.REGIST_BUSS.getCode());
            successTip.setMessage(ExceptionHelpEnum.REGIST_BUSS.getMessage());
            return successTip;
        }
        return successTip;
    }

    @PostMapping(value = "/cancel",produces = {"application/json;charset=UTF-8"})
    public Tip getCancelData(@RequestBody CancelReq cancelReq, HttpServletRequest request) {
        Tip successTip = new SuccessTip();
        String startTime = DateUtil.getAllTime();
        StopWatch stopwatch2 = new StopWatch();
        stopwatch2.start();
        String register_key = "register_server_cancel";
        //锁住当前所有的需要修改的信息，只要此人未处理完，均锁住不让预约
        if (RedisUtils.tryGetDistributedLock(register_key, "1", 20000)) {
            try {

                successTip = ValidatedCheck.validateFunc(cancelReq);
                if (successTip.getCode() == 200) {
                    successTip = clinicSequenService.cancelRegister(cancelReq);
                }

            } catch (Exception e) {
                successTip.setCode(ExceptionHelpEnum.REGIST_ERR.getCode());
                successTip.setData(e.getMessage());
            } finally {
                RedisUtils.releaseDistributedLock(register_key, "1");
                stopwatch2.stop();
                String endTime = DateUtil.getAllTime();
                ExceptionPrint.printCommErrorString("预约挂号取消",
                        JSON.toJSONString(cancelReq), String.valueOf(successTip.getCode())
                        , JSON.toJSONString(successTip.getMessage() + "=====" + successTip.getData()), startTime, endTime, String.valueOf(stopwatch2.getTotalTimeSeconds()));

            }
        } else {
            successTip.setCode(ExceptionHelpEnum.REGIST_BUSS.getCode());
            successTip.setMessage(ExceptionHelpEnum.REGIST_BUSS.getMessage());
        }
        return successTip;
    }


}
